clear all 
set more off 
set maxvar 15000 
clear matrix
set matsize 1000


* Bring in results from server
    use "$Mydirectory2/appendix_a/R2_results.dta", clear
    
    tempfile census40
    save `census40'

* Bring in 1960-1990 Censuses 
    forval i=6(1)9 {

        use "$CensusData/output/Census19`i'0_fathers_ages30to50.dta", clear //note: parentheses are absolutely necessary!
        
        drop if fatheroccej==99
        count

        egen doublet = group(fatheroccej race)
        egen triplet = group(fatheroccej race south_merge)
        egen fourplet = group(fatheroccej race south_merge edu)
        egen doublet_alt = group(race south_merge)
        
        if "`i'"=="5" {
            rename inctot HHinc
            replace perwt = slwt
        }

        tempfile census`i'
        save `census`i''   
    }

***********************************
/* Table showing R^2 as parental 
   income predictors are added */
***********************************

file open Table_SumStats using "$Mydirectory2/appendix_a/Table_R2_allsamples.tex", write replace

file write Table_SumStats "\begin{tabularx}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill} l c c c c c c}" _n
    file write Table_SumStats "\toprule" _n
    file write Table_SumStats "\addlinespace[2ex]" _n   
    file write Table_SumStats "& Occupation & Race & Occ. $\times$ race & Race $\times$ South & \shortstack{Occ. $\times$ race \\ $\times$ South} & \shortstack{Occ. $\times$ race \\ $\times$ South $\times$ edu.} \\" _n
    file write Table_SumStats "\addlinespace[1ex]" _n
    file write Table_SumStats "\midrule" _n
    
    use `census40'

    //full 1940 census
    file write Table_SumStats "1940 Census "
    
    forval i=1(1)6 {
        sum est_`i' if _n==1
        local number = `r(mean)'
        local number2: display %-09.2fc `number'    
        file write Table_SumStats "& `number2' "
    }
    
    file write Table_SumStats "  \\" _n
    file write Table_SumStats "\addlinespace[0.5ex]" _n
    
    //1940 census with 1936 fix
    file write Table_SumStats "1940 Census, 1936 fix "
    
    forval i=1(1)5 {
        sum est_`i' if _n==2
        local number = `r(mean)'
        local number2: display %-09.2fc `number'    
        file write Table_SumStats "& `number2' "
    }
    
    file write Table_SumStats " &  -- \\" _n 
    file write Table_SumStats "\addlinespace[0.5ex]" _n

    // 1960-1990 Censuses 
    forval i=6(1)9 {

        file write Table_SumStats "19`i'0 Census "
        
        use `census`i'', clear
        
        foreach var in fatheroccej race doublet doublet_alt triplet fourplet {
            quietly reg log_father_hh_income i.`var' [aw=perwt]
            
            local number =`e(r2)'
            local number2: display %-09.2fc `number'    
            file write Table_SumStats "& `number2' "
        }
        
        file write Table_SumStats " \\" _n
        file write Table_SumStats "\addlinespace[0.5ex]" _n
        
    }
    
    file write Table_SumStats "\addlinespace[1ex]" _n
    file write Table_SumStats "\bottomrule" _n
    file write Table_SumStats "\end{tabularx}" _n
    file close Table_SumStats